package com.zhuss.timer;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * sql server 数据库数据
 * 删除数据备份， 保留最近8天的备份
 * @author Administrator
 *
 */

@Component
@Configuration  
@EnableScheduling
public class DeleteBackupTask {
	
	private static Logger  LOGGER = LoggerFactory.getLogger(DeleteBackupTask.class);
	
//	@Scheduled(cron = "0/20 * * * * ?")
	@Scheduled(cron = "1 1 1 * * ?")
	private void configureTasks() {
		List<String> list = initFile();
		if (list != null && list.size() == 2) {
			File file = new File(list.get(0));
			file.delete();
//			String cmd1 ="del " + list.get(0);
//			execute(cmd1);
//			String cmd2 ="del " + list.get(1);
			try {
				Thread.sleep(6000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			file = new File(list.get(1));
			file.delete();
//			execute(cmd2);
//			System.err.println(cmd1);
//			System.err.println(cmd2);
		}
		System.err.println("DeleteBackupTask-------执行静态定时任务时间: " + LocalDateTime.now());
	}
	
	public static void main(String[] args) {
		System.out.println(initFile());
	}
	
	public static List<String> initFile() {
		// BackupPlan_Subplan_1_20210518021443.txt
		// NetTechData_dev_backup_2021_05_18_020009_0521709.bak
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -8);
		Integer year = cal.get(Calendar.YEAR);
		Integer month = cal.get(Calendar.MONTH) + 1;
		String monthStr = month < 10 ? "0" + month : "" + month;
		Integer day = cal.get(Calendar.DAY_OF_MONTH);
		String dayStr = day < 10 ? "0" + day : "" + day;
		String txtStart = "BackupPlan_Subplan_1_" + year + monthStr + dayStr;
		String bakStart = "NetTechData_dev_backup_" + year + "_" + monthStr + "_" + dayStr;
		System.out.println(txtStart);
		System.out.println(bakStart);
		File file = new File("D:\\DataBackup");
		File[] files = file.listFiles();//文件集合
		List<String> list = new ArrayList<>();
		for (int i = 0; i < files.length; i++) {//遍历集合
			if (files[i].isFile()) {//如果是文件
	            try {
	            	String path = files[i].getAbsolutePath();
	                String name = path.substring(("D:\\DataBackup").length() + 1);
	                System.out.println(name);
	                if (name.startsWith(txtStart) || name.startsWith(bakStart)) {
	                	list.add(path);
	                }
	            } catch (Exception e) {
	            	
	            }
			}
		}
		return list;
	}
	
	public void execute(String cmd) {
		Process obj = null;
         try {
        	Thread.sleep(1000);
            obj = Runtime.getRuntime().exec(cmd);
            int status = obj.waitFor();
            LOGGER.error("status: " + status);
            // LOGGER.error(JsonUtil.toJson(obj));
         } catch (Exception e) {
        	e.printStackTrace();
        	//LOGGER.error(JsonUtil.toJson(obj));
         }
	}

}
